package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PlatformServicesLevelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 平台服务级别属性联动处理类 field_name:200003681
 *
 * 平台服务级别为平台高级服务时，平台功能费默认为9
 * 平台服务级别为平台基础服务时，平台功能费不展示
 */
@Service
@AttrHandler(PlatformServicesLevelAttrHandler.class)
public class PlatformServicesLevelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr platformServicesAttr = inst.getAttr("200004024"); // 平台功能费(%)
        if ("100006417".equals(attr.getValue())) {
            LocalAttrHelper.showAndNonnull(inst, "200004024");
            if (!KiteObjectUtils.isEmpty(platformServicesAttr) && !isException(attr, inst, data)) {
                operateInstAttrService.setAttrValue(inst, "200004024", "9", data.getSceneInstId(), JSON.toJSONString(data));
            }
        }
        else {
            LocalAttrHelper.hideAndNullable(inst, "200004024");
            if (!KiteObjectUtils.isEmpty(platformServicesAttr) && !isException(attr, inst, data)) {
                operateInstAttrService.setAttrValue(inst, "200004024", KeyConsts.ZERO, data.getSceneInstId(), JSON.toJSONString(data));
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private boolean isException(KiteAttr attr, KiteInst inst, AttrData data) {
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode"))) {
            return true;
        }
        //变更业务的场景初始化过程中不校验
        if (ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return true;
        }
        return false;
    }
}
